package com.swfgui.containers
{
	import Common.core.view.CommView;
	
	import com.madbox.dragon.view.commonComponent.CommPopupDialog;
	import com.swfgui.controls.Button;
	import com.swfgui.managers.DragManager;
	import com.swfgui.managers.PopUpManager;
	import com.swfgui.utils.display.MovieClipUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	/**
	 * 弹出窗口
	 * @author llj
	 */
	public class PopUpWindow extends Canvas
	{
		private var _btnClose:Button;
		private var _title:Sprite;
		private var _enableDrag:Boolean = true;
		public var owner:DisplayObject;
		
		public function PopUpWindow(viewSource:Object=null)
		{
			super(viewSource);		
		}
		
		override protected function initialize():void
		{
			super.initialize();
			this.clipContent = false;
			this.contentBackgroundColor = -1;

			if(view["btnClose"])
			{
				_btnClose = new Button(view["btnClose"]);
				_btnClose.addEventListener(MouseEvent.CLICK, onClose);
			}
			
			if(view["title"])
			{
				_title = view["title"] as Sprite;
				_title.mouseChildren = false;
			}
			
			MovieClipUtil.stopAllMovieClip(this);
		}
		
		override public function dispose():void
		{			
			if(hasDisposed)
			{
				return;
			}
			
			if(_btnClose)
			{
				_btnClose.removeEventListener(MouseEvent.CLICK, onClose);
			}
			
			if(_title)
			{
				DragManager.unregister(_title);
			}
			
			if(owner)
			{
				owner.removeEventListener(Event.REMOVED_FROM_STAGE, onOwnerRemoveFromStage);
				owner = null;
			}
			
			super.dispose();
		}
		
		override protected function onAddedToStage():void
		{
			super.onAddedToStage();
			if(_title)
			{
				if(this.parent is CommView)
				{
					DragManager.register(_title, this.parent, DragManager.DIRECT);
				}
				else
				{
					DragManager.register(_title, this, DragManager.DIRECT);
				}
			}
		}		
	
		/**
		 * 打开窗口
		 * @param closeOther 是否关闭其它窗口
		 * @param owner 当owner关闭的时候，本窗口也会关闭
		 */
		public function show(closeOther:Boolean=true, owner:DisplayObject=null):void
		{
			if(closeOther)
			{
				CommPopupDialog.openPanel(this);
			}
			else
			{
				PopUpManager.addPopUpEx(this);
			}
			
			if(owner)
			{
				this.owner = owner;
				owner.addEventListener(Event.REMOVED_FROM_STAGE, onOwnerRemoveFromStage);
			}
		}
		
		protected function onOwnerRemoveFromStage(event:Event):void
		{
			owner.removeEventListener(Event.REMOVED_FROM_STAGE, onOwnerRemoveFromStage);
			this.hide();
		}		
	
		public function hide(hideDispose:Boolean = true):void
		{
			CommPopupDialog.closePanel(this);			
			PopUpManager.removePopUpEx(this);
			safeRemoveChild(this);
			if(hideDispose)
			{
				this.dispose();
			}
		}
		
		/**
		 * 关闭按钮鼠标单击事件处理器
		 * @param e
		 */
		protected function onClose(e:MouseEvent):void
		{
			hide();
		}
			
		public function get btnClose():Button
		{
			return _btnClose;
		}

		public function get enableDrag():Boolean
		{
			return _enableDrag;
		}

		public function set enableDrag(value:Boolean):void
		{
			_enableDrag = value;
			if(_title)
			{
				_title.mouseEnabled = value;
			}
		}

		public function get title():Sprite
		{
			return _title;
		}

	}
}